package cn.edu.scnu.socialapp.service;

import cn.edu.scnu.socialapp.dao.mapper.ArticleMapper;
import cn.edu.scnu.socialapp.dao.mapper.UserMapper;
import cn.edu.scnu.socialapp.model.entity.Article;
import cn.edu.scnu.socialapp.model.entity.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ThreadService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Async("taskExecutor")
    public void updateArticleViewCount(Article article) {
        try {
            Integer viewCounts = article.getViewCounts();
            Article articleUpdated = new Article();
            articleUpdated.setViewCounts(viewCounts + 1);
            LambdaQueryWrapper<Article> updateWrapper = new LambdaQueryWrapper<>();
            // 根据id更新
            updateWrapper.eq(Article::getArticleId, article.getArticleId());
            // 设置一个为了在多线程的环境下线程安全的乐观锁
            // 改之前再确认这个值有没有被其他线程抢先修改，类似于CAS操作，加个自旋，加个循环就是cas
            updateWrapper.eq(Article::getViewCounts, viewCounts);
            // update article set view_count=100 where view_count=99 and id=111
            // 实体类加更新条件
            articleMapper.update(articleUpdated, updateWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Async("taskExecutor")
    public void updateArticleCommentCount(Long articleId) {
        try {
            Article article = articleMapper.selectById(articleId);
            Integer commentCounts = article.getCommentCounts();
            Article articleUpdated = new Article();
            articleUpdated.setCommentCounts(commentCounts + 1);
            LambdaQueryWrapper<Article> updateWrapper = new LambdaQueryWrapper<>();
            // 根据id更新
            updateWrapper.eq(Article::getArticleId, article.getArticleId());
            // 设置一个为了在多线程的环境下线程安全的乐观锁
            // 改之前再确认这个值有没有被其他线程抢先修改，类似于CAS操作，加个自旋，加个循环就是cas
            updateWrapper.eq(Article::getCommentCounts, commentCounts);
            // update article set comment_count=100 where comment_count=99 and id=111
            // 实体类加更新条件
            articleMapper.update(articleUpdated, updateWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Async("taskExecutor")
    public void updateUserLastLoginTime(User user){
        try {
            User updatedUser = new User();
            updatedUser.setLastLogin(System.currentTimeMillis());
            LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<User>();
            updateWrapper.eq(User::getUserId, user.getUserId());
            userMapper.update(updatedUser,updateWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
